package ru.infosystem.client.dao;

import ru.indosystem.client.frameutils.comparator.SortedByFio;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.infosystem.client.BDConnection;
import ru.infosystem.client.data.Group;
import ru.infosystem.client.data.Message;
import ru.infosystem.client.data.ServerException;
import ru.infosystem.client.data.Student;
import ru.infosystem.client.model.StudentTableModel;
import ru.infosystem.client.task.Command;
import ru.infosystem.client.task.Task;

/**
 *
 * @author akoshi
 */
public class StudentDAO {

    public static StudentTableModel listByGroup(BDConnection connection,
            Group group) throws IOException, ClassNotFoundException {

        ArrayList<Student> list = null;

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        connection.getOutputStream().flush();

        out.writeObject(new Task(Command.LIST_STUDENTS_OF_GROUP, group));

        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        list = (ArrayList<Student>) o;

        Student[] arr = new Student[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
            System.err.println(arr[i].getName());
        }

        Arrays.sort(arr, new SortedByFio()); // нехорошо здесь размещать, но для теста сойдёт
        return new StudentTableModel(arr);
    }

    public static boolean deleteStudent(BDConnection connection, Student student) throws IOException {

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        out.writeObject(new Task(Command.DELETE_STUDENT, student));

        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ((Boolean) o).booleanValue();
    }

    public static boolean addStudent(BDConnection connection, Student student) throws IOException {

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        out.writeObject(new Task(Command.ADD_STUDENT, student));
        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ((Boolean) o).booleanValue();
    }

    public static StudentTableModel findStudents(BDConnection connection, String template) throws IOException, ClassNotFoundException {

        ObjectOutputStream out = connection.getObjOutputStream();

        ObjectInputStream in = connection.getObjInputStrem();

        out.writeObject(new Task(Command.FIND, new Message(template)));

        Object o = null;
        try {
            o = in.readObject();
            if (o instanceof ServerException) {
                throw (ServerException) o;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        ArrayList<Student> list = (ArrayList<Student>) o;
        Student[] arr = new Student[list.size()];

        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
            System.err.println(arr[i].getName());
        }

        return new StudentTableModel(arr);

    }
}
